package com.codi.bus.core.service.impl;

import com.codi.base.util.DateUtils;
import com.codi.base.util.StringUtil;
import com.codi.bus.constant.GlobalConstant;
import com.codi.bus.core.dao.CdUserShareDao;
import com.codi.bus.core.dao.OrganProfileDao;
import com.codi.bus.core.dao.UserDao;
import com.codi.bus.core.domain.CdUserShare;
import com.codi.bus.core.domain.OrganProfile;
import com.codi.bus.core.domain.User;
import com.codi.bus.core.util.ValueUtil;
import com.codi.fcloud.ufx.UfxSDKFactory;
import com.codi.fcloud.ufx.constant.UrlConstants;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: zengxl
 * @Description:
 * @Date: 2018/5/4 20:07
 */
@Service
public class LocaldataUserShareService   extends UfxSDKFactory {


    @Autowired
    private CdUserShareDao cdUserShareDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private OrganProfileDao organProfileDao;
    @Transactional
    public void execUserShare(String clientId){
        Map<String, Object> request  = new HashMap<>();
        request.put("request_num", GlobalConstant.MAX_REQUEST_NUM);
        request.put("client_id", clientId);
        request.put("qry_beginrownum", 1);
        request.put("reqry_recordsum_flag", 1);
        List<Map<String, Object>> response = getUfxSDK().query(getTarget(), request);
        request.put("reqry_recordsum_flag","0");
        getNextResponse(request, response, GlobalConstant.MAX_REQUEST_NUM);
        for(Map<String, Object> map:response){
            String trade_acco = ValueUtil.getObjStr(map.get("trade_acco"));
            String fund_code = ValueUtil.getObjStr(map.get("fund_code"));
            String ta_acco = ValueUtil.getObjStr(map.get("ta_acco"));
            if(trade_acco.equals("") || fund_code.equals("") || ta_acco.equals("")){
                continue;
            }

        }

    }

    /**
     * 任务  1
     */
    @Transactional
    public void init(){
        //先把原来是上次的数据标识变成历史数据标识
        cdUserShareDao.updateShareOld();
//        //再把当前的数据标识变成上次数据标识
//        cdUserShareDao.updateShare();
        //获取普通用户的用户编号

        CdUserShare userShare = CdUserShare.builder().createTime(DateUtils.formatDate4(DateUtils.addDay(new Date(), -30))).build();
        cdUserShareDao.updateShareExpired(userShare);
        List<User> all = Lists.newArrayList();
        List<User> users = userDao.getAllUser();
        if(users != null && users.size() > 0){
            all.addAll(users);
        }
        //获取机构用户编号
        List<OrganProfile> organProfileList = organProfileDao.queryAllOrganProfiles();
        for(OrganProfile file:organProfileList){
            User temp = new User();
            temp.setUserId(file.getUserId());
            temp.setExternalClientId(file.getExternalClientId());
            all.add(temp);
        }
        Map<String, Object> request  = new HashMap<>();
        request.put("request_num", GlobalConstant.MAX_REQUEST_NUM);
        for (User user : all) {
            if (user == null || StringUtil.isEmpty(user.getExternalClientId())) {
                continue;
            }
            request.put("client_id", user.getExternalClientId());
            request.put("qry_beginrownum", 1);
            request.put("reqry_recordsum_flag", 1);
            List<Map<String, Object>> response = getUfxSDK().query(getTarget(), request);
            request.put("reqry_recordsum_flag","0");
            getNextResponse(request, response, GlobalConstant.MAX_REQUEST_NUM);
            for(Map<String, Object> map:response){
                String trade_acco = ValueUtil.getObjStr(map.get("trade_acco"));
                String fund_code = ValueUtil.getObjStr(map.get("fund_code"));
                String ta_acco = ValueUtil.getObjStr(map.get("ta_acco"));
                if(trade_acco.equals("") || fund_code.equals("") || ta_acco.equals("")){
                    continue;
                }

                CdUserShare cdUserShare = new CdUserShare();
                cdUserShare.setTradeAcco(trade_acco);
                cdUserShare.setFundCode(fund_code);
                cdUserShare.setTaAcco(ta_acco);
                cdUserShare.setUserId(user.getUserId().intValue());
                cdUserShare.setClientId(user.getExternalClientId());

                String share_type = ValueUtil.getObjStr(map.get("share_type"));
                cdUserShare.setShareType(share_type);
                String current_share = ValueUtil.getObjStr(map.get("current_share"));
                if(current_share.equals("")){
                    current_share = "0";
                }
                cdUserShare.setCurrentShare(new BigDecimal(current_share));
                String enable_shares = ValueUtil.getObjStr(map.get("enable_shares"));
                if(enable_shares.equals("")){
                    enable_shares = "0";
                }
                cdUserShare.setEnableShares(new BigDecimal(enable_shares));
                String frozen_share = ValueUtil.getObjStr(map.get("frozen_share"));
                if(frozen_share.equals("")){
                    frozen_share = "0";
                }
                cdUserShare.setFrozenShare(new BigDecimal(frozen_share));
                String auto_buy = ValueUtil.getObjStr(map.get("auto_buy"));
                cdUserShare.setAutoBuy(auto_buy);
                String business_frozen_amount = ValueUtil.getObjStr(map.get("business_frozen_amount"));
                if(business_frozen_amount.equals("")){
                    business_frozen_amount = "0";
                }
                cdUserShare.setBusinessFrozenAmount(new BigDecimal(business_frozen_amount));
                String unpaid_income = ValueUtil.getObjStr(map.get("unpaid_income"));
                if(unpaid_income.equals("")){
                    unpaid_income = "0";
                }
                cdUserShare.setUnpaidIncome(new BigDecimal(unpaid_income));
                String capital_mode = ValueUtil.getObjStr(map.get("capital_mode"));
                cdUserShare.setCapitalMode(capital_mode);
                String bank_no = ValueUtil.getObjStr(map.get("bank_no"));
                cdUserShare.setBankNo(bank_no);
                String bank_account = ValueUtil.getObjStr(map.get("bank_account"));
                cdUserShare.setBankAccount(bank_account);
                String available_due_share = ValueUtil.getObjStr(map.get("available_due_share"));
                if(available_due_share.equals("")){
                    available_due_share = "0";
                }
                cdUserShare.setAvailableDueShare(new BigDecimal(available_due_share));
                String quick_exceed_enable_share = ValueUtil.getObjStr(map.get("quick_exceed_enable_share"));
                if(quick_exceed_enable_share.equals("")){
                    quick_exceed_enable_share = "0";
                }
                cdUserShare.setQuickExceedEnableShare(new BigDecimal(quick_exceed_enable_share));
                String lock_share = ValueUtil.getObjStr(map.get("lock_share"));
                if(lock_share.equals("")){
                    lock_share = "0";
                }
                cdUserShare.setLockShare(new BigDecimal(lock_share));
                String c_flag = ValueUtil.getObjStr(map.get("c_flag"));
                cdUserShare.setCFlag(c_flag);
                String fund_status = ValueUtil.getObjStr(map.get("fund_status"));
                cdUserShare.setFundStatus(fund_status);
                String cust_type = ValueUtil.getObjStr(map.get("cust_type"));
                cdUserShare.setCustType(cust_type);
                String ofund_type = ValueUtil.getObjStr(map.get("ofund_type"));
                cdUserShare.setOfundType(ofund_type);
                cdUserShare.setCreateTime(DateUtils.getCurrTime());
                cdUserShare.setLastupdateTime("1");
                cdUserShareDao.insert(cdUserShare);
            }
        }
    }

    @Override
    protected String getTarget() {
        return UrlConstants.PREFIX_SALE + UrlConstants.SHARE_QRY_101;
    }
}
